/*****************************************************************************
*
*  PROJECT:     SlipStream
*               (CParticleEmitter - particle emitter)
*  LICENSE:     GNU General Public License v3 - http://www.gnu.org/licenses/gpl.html

*  DEVELOPERS:  Philip Farquharson	<B00137077@studentmail.uws.ac.uk>
*				John Gray			<B00145524@studentmail.uws.ac.uk>		
*				Zohaib Bhatti		<B00104280@studentmail.uws.ac.uk>
*				Euan Hislop			<B00138636@studentmail.uws.ac.uk>
*
*****************************************************************************/
#include "CParticleEmitter.h"

CParticleEmitter::CParticleEmitter( CVector3 vecPosition, CVector3 vecGravity, CVector3 vecDirectionSign, CColor col )
{
	m_vecPosition = vecPosition;

	float dxsign = vecDirectionSign.getX();
	float dysign = vecDirectionSign.getY();
	float dzsign = vecDirectionSign.getZ();

	for (int i = 0; i < MAX_PARTICLES; i++)
	{
		float dx = dxsign * float((rand()%50)-56.0f)*10.0f * 0.5f;
		float dy = dysign * float((rand()%50)-56.0f)*10.0f * 0.5f;
		float dz = dzsign * float((rand()%50)-56.0f)*10.0f * 0.5f;

		CParticle *pParticle = new CParticle(vecPosition, vecGravity, CVector3(dx, dy, dz), vecDirectionSign, col);
		m_particles.push_back(pParticle);
	}
}

void CParticleEmitter::Draw ( void )
{
	glPushMatrix();	

	CCamera* camera = CCamera::GetSingletonPtr();
	CVector3 cameraPos(camera->getXPosition(), camera->getYPosition(), camera->getZPosition());
	int limit = MAX_PARTICLES - (int)(m_vecPosition - cameraPos).length() * 4;

	if ( limit < MIN_PARTICLES)
		limit = MIN_PARTICLES;

	int i = 0;
	std::list<CParticle*>::const_iterator iter = m_particles.begin();
	for ( ; iter != m_particles.end (); ++iter )
	{
		if ( i == limit )
			break;

		CParticle* pParticle = static_cast < CParticle* > ( *iter );

		assert ( pParticle );

		pParticle->Draw ( );

		i++;
	}
	glPopMatrix();
}

void CParticleEmitter::Update ( void )
{
	CCamera* camera = CCamera::GetSingletonPtr();
	CVector3 cameraPos(camera->getXPosition(), camera->getYPosition(), camera->getZPosition());
	int limit = MAX_PARTICLES - (int)(m_vecPosition - cameraPos).length() * 4;

	if ( limit < MIN_PARTICLES)
		limit = MIN_PARTICLES;

	int i = 0;
	std::list<CParticle*>::const_iterator iter = m_particles.begin();
	for ( ; iter != m_particles.end (); ++iter )
	{
		if ( i == limit )
			break;

		CParticle* pParticle = static_cast < CParticle* > ( *iter );

		assert ( pParticle );

		pParticle->Update ( );

		i++;
	}
}
